//
//  CZUserAccount.swift
//  CY--weiBo
//
//  Created by mac on 15/11/27.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
//CustomStringConvertible
//将对象归档到沙盒
class CZUserAccount: NSObject,NSCoding {
    
    
    //MARK: - 属性
    //用户是否登录
    class var userLogin: Bool {
        get {
        
        return CZUserAccount.loadUserAccount() != nil
        }
    }
    //用于调用access_token,接口获取授权后的access_token
    var access_token: String?
    //access_token的生命周期,单位是秒数,在kVC 中基本数据类型需要设置初始值
    var expires_in: NSTimeInterval = 0 {
        didSet {
            //设置过期时间
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    // 保存过期时间
    var expiresDate: NSDate?
    
    //当前授权用户的UID
    var uid: String?
    
    //用户昵称
    var screen_name: String?
    
    //用户头像地址(大图) ,180 x 180像素,图片的网络地址
    var avatar_large: String?
    
    //字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    ///加载用户信息
    func loadUserInfo() {
        //获取用户数据
        CZNetworkTool.sharedInstance.loadUserInfo  {(result, error) -> () in
            if error != nil || result == nil {
                print("加载数据失败: \(error)")
                return
            }
            print("数据加载成功")
            
            //设置属性
            self.screen_name = result!["screen_name"] as? String
            self.avatar_large = result!["avatar_large"] as? String
            //对象获取的2个属性,更新userAccount账户
            CZUserAccount.userAccount = self
            
            //保存起来
            self.saveAccount()
        }
    
    }
    //保存路径 staic 类方法也可以访问,对象方法访问需要使用类名,属性名
    static let plistPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! + "/account.plist"
    
    
    /// 保存账户信息到沙盒
    func saveAccount() {
        //归档 当前类
        NSKeyedArchiver.archiveRootObject(self, toFile:CZUserAccount.plistPath)
    }
    
    //内存中的账户
    private static var userAccount: CZUserAccount?
    
    //读取 保存在沙盒里面的账户信息
    class func loadUserAccount() -> CZUserAccount? {
        
        //1.判断内存中的账户是否可用
        if userAccount == nil {
            //内存中没有账户,到沙盒加载一个账户
            userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(plistPath) as? CZUserAccount
            print("从沙盒加载账户")
        
        }else{
            print("直接从内存中获取到账户")
        }
        //2.判断内存中是否有用账户,有账户,判断时间是否过期
        if userAccount != nil && userAccount?.expiresDate?.compare(NSDate()) ==
            NSComparisonResult.OrderedDescending {
            //有账户并且没有过期,是一个可用的账户
            return userAccount
        }
        
        return nil
    }
    /// 解档
    required init?(coder aDecder: NSCoder) {
        access_token = aDecder.decodeObjectForKey("access_token") as? String
        expires_in = aDecder.decodeDoubleForKey("expires_in")
        expiresDate = aDecder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecder.decodeObjectForKey("uid") as? String
        screen_name = aDecder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecder.decodeObjectForKey("avatar_large") as? String
    }
    /// 归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    /// 当KVC字典的key再模型中找不到对应的属性,可以空实现
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    override var description: String {
        return "access_token: \(access_token), expires_in:\(expires_in), uid = \(uid), expiresDate:\(expiresDate)"

    }
    
    
    
    
    

}
